import 'package:get/get.dart';
import '../../../services/cartService.dart';
import '../../../services/userService.dart';
import '../../../services/storage.dart';

class CartController extends GetxController {
  RxList cartList = [].obs;
  RxBool checkedAllBox = false.obs;
  RxDouble totalAmount = 0.0.obs;
  RxBool isEdit = false.obs;

  void getCartListData() async {
    var tempList = await CartService.getCartList();
    cartList.value = tempList;
    calcTotalAmount();
    update();
  }

  // 增加数量
  void incCartNum(cartItem) {
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["_id"] == cartItem["_id"] &&
          cartList[i]["selectedAttr"] == cartItem["selectedAttr"]) {
        cartList[i]["count"]++;
      }

      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartService.setCartList(tempList);

    update();
  }

  // 减少数量
  void decCartNum(cartItem) {
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["_id"] == cartItem["_id"] &&
          cartList[i]["selectedAttr"] == cartItem["selectedAttr"]) {
        if (cartList[i]["count"] > 1) {
          cartList[i]["count"]--;
        } else {
          Get.snackbar('提示！', "购物车数量已经到最小了");
        }
      }
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartService.setCartList(tempList);
    update();
  }

  // 选中&取消选中
  void checkCartItem(cartItem) {
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["_id"] == cartItem["_id"] &&
          cartList[i]["selectedAttr"] == cartItem["selectedAttr"]) {
        cartList[i]["checked"] = !cartList[i]["checked"];
      }
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartService.setCartList(tempList);
    calcTotalAmount();
    update();
  }

  // 全选&反选
  void checkedAllFunc(value) {
    checkedAllBox.value = value;
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      cartList[i]["checked"] = value;
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartService.setCartList(tempList);
    calcTotalAmount();
    update();
  }

  // 判断是否全选
  bool isCheckedAll() {
    if (cartList.isNotEmpty) {
      for (var i = 0; i < cartList.length; i++) {
        if (cartList[i]["checked"] == false) {
          return false;
        }
      }
      return true;
    }
    return false;
  }

  // 计算总金额
  void calcTotalAmount() {
    var tempTotalAmount = 0.0;
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["checked"] == true) {
        tempTotalAmount +=
            cartList[i]["price"].toDouble() * cartList[i]["count"].toDouble();
      }
    }

    totalAmount.value = tempTotalAmount;
  }

  getCheckListData() {
    List tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["checked"] == true) {
        tempList.add(cartList[i]);
      }
    }

    return tempList;
  }

  // 去结算
  checkout() async {
    var isLogin = await UserService.getUserLoginState();
    if (isLogin) {
      List checkList = getCheckListData();
      if (checkList.isNotEmpty) {
        // 保存要结算的商品
        Storage.setData("checkoutList", checkList);
        Get.toNamed("/checkout");
      } else {
        Get.snackbar("提示", "购物车中没有要结算的商品");
      }
    } else {
      Get.snackbar("提示", "您还没有登录，请先登录");
      // 跳转到登录页面
      Get.toNamed("/code-login-step-one");
    }
  }

  // 改变edit
  changeEditState() {
    isEdit.value = !isEdit.value;
    update();
  }

  // 删除购物车数据
  deleteCartData() {
    List tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]["checked"] == false) {
        tempList.add(cartList[i]);
      }
    }
    // 把没有选中的商品保存在cart里面
    cartList.value = tempList;
    CartService.setCartList(tempList);
    update();
  }
}
